package com.gao.shiro.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.gao.shiro.demo.pojo.User;
import com.gao.shiro.demo.service.UserService;
import commomresult.CommonResult;
import commonjwt.JwtUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author gaofeng
 * date 2021年10月09日 20:43
 */
@Controller
@CrossOrigin
public class IndexController {

    @Resource
    private UserService userService ;

    @Resource
    private RedisTemplate redisTemplate ;

    @RequestMapping({"/index","/"})
    public String toIndex(Model model) {
        model.addAttribute("msg", "hello shiro!");
        return "/index";
    }

    @PostMapping("/user/add")
    public String toAdd() {
        return "/user/add";
    }

    @PostMapping("/user/update")
    public String toUpdate() {
        return "/user/update";
    }

    @RequiresRoles(logical = Logical.OR, value = {"user", "admin"})
    @RequestMapping("/toLogin")
    public String toLogin() {
        return "/login" ;
    }

    @RequiresRoles(logical = Logical.OR, value = {"user", "admin"})
    @RequestMapping("/getPayment")
    @ResponseBody
    public CommonResult<User> getPayment() {
        User gaofeng = userService.getUser("gaofeng");
        CommonResult<User> result = new CommonResult<>();
        result.setData(gaofeng) ;
        result.setMsg("请求成功！");
        result.setCode("10000") ;
        return result;
    }


    @RequestMapping("/login")
    public String login(String username, String password, Model model) {
        // 获取当前用户
        Subject subject = SecurityUtils.getSubject();
        // 封装用户的登录数据
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, password);

        try {
            subject.login(usernamePasswordToken) ;
            Map<String, Object> userMap = new HashMap<>(2);
            userMap.put("userName", username) ;
            userMap.put("password", password) ;
            // 生成token
            String token = JwtUtils.createToken(JSONObject.toJSONString(username));
            // 存redis
            redisTemplate.opsForValue().set(token, JSON.toJSONString(userMap, SerializerFeature.WriteNullStringAsEmpty), 24 *60 *60 *1000);

            /*if (存redis成功) {
                设置并返回给前端
            } else {

            }*/
            return "/index" ;
        } catch (UnknownAccountException e) {
            model.addAttribute("msg", "用户名错误") ;
            return "/login" ;
        } catch (IncorrectCredentialsException e) {
            model.addAttribute("msg", "密码错误") ;
            return "/login" ;
        }

    }
}
